package cn.com.lyj6851.modules.shop.controller;

import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import cn.com.lyj6851.modules.shop.domain.YdySystemStore;
import cn.com.lyj6851.modules.shop.domain.YdySystemStoreStaff;
import cn.com.lyj6851.modules.shop.service.YdySystemStoreService;
import cn.com.lyj6851.modules.shop.service.YdySystemStoreStaffService;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemStoreStaffDto;
import cn.com.lyj6851.modules.shop.service.dto.YdySystemStoreStaffQueryCriteria;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
* @author hupeng
* @date 2020-03-22
*/
@Api(tags = "门店店员管理")
@RestController
@RequestMapping("/api/ydySystemStoreStaff")
public class SystemStoreStaffController {

    private final YdySystemStoreStaffService yxSystemStoreStaffService;
    private final YdySystemStoreService yxSystemStoreService;

    private final IGenerator generator;

    public SystemStoreStaffController(YdySystemStoreService yxSystemStoreService,YdySystemStoreStaffService yxSystemStoreStaffService, IGenerator generator) {
        this.yxSystemStoreService = yxSystemStoreService;
        this.yxSystemStoreStaffService = yxSystemStoreStaffService;
        this.generator = generator;
    }

    @AopLog("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ydySystemStoreStaff:list')")
    public void download(HttpServletResponse response, YdySystemStoreStaffQueryCriteria criteria) throws IOException {
        yxSystemStoreStaffService.download(generator.convert(yxSystemStoreStaffService.queryAll(criteria), YdySystemStoreStaffDto.class), response);
    }

    @GetMapping
    @AopLog("查询门店店员")
    @ApiOperation("查询门店店员")
    @PreAuthorize("@el.check('ydySystemStoreStaff:list')")
    public ResponseEntity<Object> getYdySystemStoreStaffs(YdySystemStoreStaffQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxSystemStoreStaffService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @AopLog("新增门店店员")
    @ApiOperation("新增门店店员")
    @PreAuthorize("@el.check('ydySystemStoreStaff:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YdySystemStoreStaff resources){
        YdySystemStore systemStore = yxSystemStoreService.getOne(Wrappers.<YdySystemStore>lambdaQuery()
                .eq(YdySystemStore::getId,resources.getStoreId()));
        resources.setStoreName(systemStore.getName());
        return new ResponseEntity<>(yxSystemStoreStaffService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @AopLog("修改门店店员")
    @ApiOperation("修改门店店员")
    @PreAuthorize("@el.check('ydySystemStoreStaff:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YdySystemStoreStaff resources){
        YdySystemStore systemStore = yxSystemStoreService.getOne(Wrappers.<YdySystemStore>lambdaQuery()
                .eq(YdySystemStore::getId,resources.getStoreId()));
        resources.setStoreName(systemStore.getName());
        yxSystemStoreStaffService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @AopLog("删除门店店员")
    @ApiOperation("删除门店店员")
    @PreAuthorize("@el.check('ydySystemStoreStaff:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxSystemStoreStaffService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
